/*
 * ipassbook 0.6.0
 * Copyright (C)2009 wateray and contributors
 * wateray@gmail.com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

package com.wateray.ipassbook.ui.dialog.wizard;

import java.awt.Dimension;
import java.awt.Insets;
import java.text.MessageFormat;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;

import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JTextField;

import com.wateray.ipassbook.commom.CommomTool;
import com.wateray.ipassbook.domain.Currency;
import com.wateray.ipassbook.domain.Entity;
import com.wateray.ipassbook.domain.form.PassbookForm;
import com.wateray.ipassbook.kernel.service.CurrencyService;
import com.wateray.ipassbook.ui.model.PassbookComboBoxModel;
import com.wateray.ipassbook.util.CalendarUtils;
import com.wateray.ipassbook.util.LanguageLoader;

/**
 * @author wateray
 * @create 2009-5-16
 */
public class PassbookWizardPageTwo extends AbstractWizardPage {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private JLabel initDataLabel;
	private JTextField initDataText;

	private JLabel initAmountLabel;
	private JTextField initAmountText;

	private JLabel currencyLabel;
	private JComboBox currencyComboBox;
	private PassbookComboBoxModel currencyComboxMode;

	private JLabel describeLabel;

	public PassbookWizardPageTwo(IWizardDialog wizardDialog) {
		super(wizardDialog);
		initPage();
		initState();
	}

	protected void initPage() {
		super.initPage();

		// getInitDataText
		constraints.insets = new Insets(15, left, bottom, right);
		constraints.gridy = ++gridy;
		this.add(getInitDataLabel(), constraints);

		/** getInitDataText. */
		constraints.gridx = 1;
		this.add(getInitDataText(), constraints);

		// spaceLabel
		constraints.gridx = 2;
		constraints.weightx = 1.0;
		this.add(new JLabel(), constraints);
		constraints.weightx = 0.0;

		//
		constraints.insets = inserts;
		constraints.gridx = 0;
		constraints.gridy = ++gridy;
		this.add(getInitAmountLabel(), constraints);

		constraints.gridx = 1;
		this.add(getInitAmountText(), constraints);

		//
		constraints.gridx = 0;
		constraints.gridy = ++gridy;
		this.add(getCurrencyLabel(), constraints);

		constraints.gridx = 1;
		this.add(getCurrencyComboBox(), constraints);

		// spaceLabelBottom
		constraints.insets = new Insets(60, left, bottom, right);
		constraints.weighty = 1.0;
		constraints.gridwidth = 3;
		constraints.gridx = 0;
		constraints.gridy = ++gridy;
		this.add(getDescribeLabel(), constraints);
		constraints.weighty = 0.0;
	}

	private void initState(){
		getInitDataText().requestFocusInWindow();
	}
	/**
	 * @return the initDataLabel
	 */
	private JLabel getInitDataLabel() {
		if (initDataLabel == null) {
			initDataLabel = new JLabel(LanguageLoader.getString("Init_Data"));
		}
		return initDataLabel;
	}

	/**
	 * @return the initDataText
	 */
	private JTextField getInitDataText() {
		if (initDataText == null) {
			initDataText = new JTextField();
			initDataText.setPreferredSize(new Dimension(150, initDataText
					.getPreferredSize().height));
			
			initDataText.setText(
					CalendarUtils.getTodayInstance().getBeginDay());
		}
		return initDataText;
	}

	/**
	 * @return the initAmountLabel
	 */
	private JLabel getInitAmountLabel() {
		if (initAmountLabel == null) {
			initAmountLabel = new JLabel(LanguageLoader
					.getString("Init_Amount"));
		}
		return initAmountLabel;
	}

	/**
	 * @return the initAmountText
	 */
	private JTextField getInitAmountText() {
		if (initAmountText == null) {
			initAmountText = new JTextField();
			initAmountText.setHorizontalAlignment(JTextField.RIGHT);
		}
		return initAmountText;
	}

	/**
	 * @return the currencyLabel
	 */
	private JLabel getCurrencyLabel() {
		if (currencyLabel == null) {
			currencyLabel = new JLabel(LanguageLoader.getString("Currency"));
		}
		return currencyLabel;
	}

	/**
	 * @return the currencyComboBox
	 */
	private JComboBox getCurrencyComboBox() {
		if (currencyComboBox == null) {
			currencyComboBox = new JComboBox();
			currencyComboxMode = new PassbookComboBoxModel();
			CurrencyService service = new CurrencyService();
			List<Currency> list = service.getCurrencyList();
			Vector<Entity> v = new Vector<Entity>();
			Iterator<Currency> it = list.iterator();
			while(it.hasNext()){
				v.add((Entity)it.next());
			}
			currencyComboxMode.setData(v);
			currencyComboBox.setModel(currencyComboxMode);
		}
		return currencyComboBox;
	}

	private JLabel getDescribeLabel() {
		if (describeLabel == null) {
			describeLabel = new JLabel(LanguageLoader
					.getString("PassbookWizardPageTwoDescribe"));
		}
		return describeLabel;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.wateray.ipassbook.ui.dialog.wizard.IWizardPage#checkData()
	 */
	@Override
	public String checkData() {
		// TODO Auto-generated method stub
		String errorMessage = "";
		logger.debug(this.getClass().getSimpleName() + " is invoked.");
		String initDataText = getInitDataText().getText();
		
		if(initDataText == null || initDataText.trim().equals("")){
			errorMessage = MessageFormat.format(LanguageLoader
					.getString("empty"), getInitDataLabel().getText());
			
			getInitDataText().requestFocus();
			return errorMessage;
		}
		
		String initAmountText =  getInitAmountText().getText(); 
		if(initAmountText == null || initAmountText.trim().equals("")){
			errorMessage = MessageFormat.format(LanguageLoader
					.getString("empty"), getInitAmountLabel().getText());
			getInitAmountText().requestFocus();
			return errorMessage;
		}
		//isNumber check.
		if(!CommomTool.isNumber(initAmountText)){
			errorMessage = MessageFormat.format(LanguageLoader
					.getString("NumberFormatException"), getInitAmountLabel().getText());
			getInitAmountText().requestFocus();
			return errorMessage;
		}
		
		String currency = (String) getCurrencyComboBox().getSelectedItem();
		if(currency == null || currency.trim().equals("")){
			errorMessage = MessageFormat.format(LanguageLoader
					.getString("empty"), getCurrencyLabel().getText());
			
			getCurrencyComboBox().requestFocus();
			return errorMessage;
		}

		return errorMessage;
	}

	@Override
	public void processData(Entity entity) {
		// TODO Auto-generated method stub
		PassbookForm passbookEntity = (PassbookForm)entity;
		
		//create date.
		passbookEntity.setCreateDate(getInitDataText().getText());
		
		//amount.
		passbookEntity.setCapital(Double.parseDouble(getInitAmountText().getText().trim()));
		
		//currency
		passbookEntity.setCurrencyId(((Currency)currencyComboxMode.getSelectedObject()).getCurrencyId());
		
		//currency
		passbookEntity.setCurrencyName(((Currency)currencyComboxMode.getSelectedObject()).getCurrencyName());
		
		
	}

	@Override
	public void beforeShowing() {
		// TODO Auto-generated method stub
		
	}

}
